﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using System.Web.Security;
using System.IO;
using Newtonsoft.Json;
using System.Collections;
using System.Data.SqlClient;
using Media.Core;
using Media.Core.Data;
using Media.Core.Models.Admin;
using Media.Core.Helpers;

namespace Media.Web.Controllers
{
    public class AdminController : Controller
    {
        //
        // GET: /Admin/

        public ActionResult Index()
        {
            var Menus = Connections.GetDataTable("Admin_Get_Menus");
            var MovieInfo = Connections.GetDataRow("Admin_Get_MovieInfo");
            var TvInfo = Connections.GetDataRow("Admin_Get_TvInfo");

            var AdminList = new AdminIndex();

            foreach (DataRow menu in Menus.Rows)
            {
                AdminList.Menus.Add(new Menu
                {
                    Id = Convert.ToInt32(menu["Id"].ToString()),
                    MenuText = menu["MenuText"].ToString(),
                    PageName = menu["PageName"].ToString(),
                    ControllerName = menu["ControllerName"].ToString(),
                    IsActive = Convert.ToBoolean(menu["IsActive"].ToString()),
                    RoleId = Convert.ToInt32(menu["RoleId"].ToString())
                });
            }

            AdminList.TotalTvCount = TvInfo["TotalTvShows"].ToString();
            AdminList.TotalMovieCount = MovieInfo["TotalMovies"].ToString();
            
            return View(AdminList);
        }

        public ActionResult UpdateDirectories()
        {
            var sr = new StreamReader(Request.InputStream);
            var contentString = sr.ReadToEnd();
            var parameterobjects = JsonConvert.DeserializeObject<Media.Core.Models.Admin.Directory>(contentString);

            ArrayList paramsList = new ArrayList
            {
                new SqlParameter("Id", parameterobjects.Id),
                new SqlParameter("Name", parameterobjects.Name),
                new SqlParameter("Directory", parameterobjects.MediaDirectory)
            };

            var Results = Media.Core.Helpers.Main.InsertDB.Insert(paramsList, "Admin_Update_Directories");

            return Json(Results, JsonRequestBehavior.AllowGet);

        }

    }
}
